package com.xiaoyu.lanling.feature.moment

import android.os.Bundle
import com.xiaoyu.base.app.AppContext
import com.xiaoyu.base.data.UserData
import com.xiaoyu.base.model.User
import com.xiaoyu.lanling.R
import com.xiaoyu.lanling.activity.base.AppCompatToolbarActivity
import com.xiaoyu.lanling.common.Constant
import com.xiaoyu.lanling.feature.moment.controller.my.MyMomentController
import com.xiaoyu.lanling.feature.moment.data.detail.MyMomentListData
import kotlinx.android.synthetic.main.activity_my_moment.*

class MyMomentActivity : AppCompatToolbarActivity() {

    private lateinit var user: User
    private lateinit var listController: MyMomentController

    override fun onCreateSafelyAfterAppFinishInit(savedInstanceState: Bundle?) {
        setLightStatusBar()
        setContentView(R.layout.activity_my_moment)
        initView(savedInstanceState)
        initData()
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        val user = intent.getSerializableExtra(Constant.IntentKey.KEY_USER) as? User
        outState.putSerializable(Constant.IntentKey.KEY_USER, user)
    }

    private fun initData() {
        val listData = MyMomentListData(user.uid)
        listController = MyMomentController.create(this, listData, swipe_refresh_layout, recyclerview)
        listController.loadFirstPageData()
    }

    private fun initView(savedInstanceState: Bundle?) {
        initToolbar()

        user = (savedInstanceState?.getSerializable(Constant.IntentKey.KEY_USER) as? User) ?: intent.getSerializableExtra(Constant.IntentKey.KEY_USER) as User
        title = if (UserData.getInstance().isSelf(user.uid)) AppContext.getString(R.string.moment_my_moment) else AppContext.getString(R.string.moment_his_moment)
        recyclerview.itemAnimator = null
    }
}